perm filename EPUT.2[EAL,HE] blob sn#708933 filedate 1983-05-01 generic text, type C, neo UTF8
COMMENT ⊗   VALID 00007 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00002 00002	{$NOMAIN	Editor routines to Put things on the display }
C00031 00003	{ Externally defined routines from elsewhere: }
C00033 00004	(* print routines: putChar, put5, put10, putLine *)
C00042 00005	(* aux print routines: putReal, putInt, putVec, putTrans, putStrng, putTlist *)
C00049 00006	(* expression related routine: getExprLength *)
C00050 00007	(* Expression output routine:  putExpr *)
C00058 ENDMK
C⊗;
{$NOMAIN	Editor routines to Put things on the display }

(* definition of record types & global variables used by AL *)

const 

  maxLines = 28;
  maxPPLines = 18;
  maxBpts = 25;
  maxTBpts = 20;	(* max could be exceeded by huge case stmnt *)
  listinglength = 2000;	(* Length of Listingarray *)

type

(* random type declarations for OMSI/SAIL compatibility *)

 ascii = char;
 atext = text;
 byte = 0..255;	

(* Here are all the pointer-type definitions.  Since the various 	*)
(* records reference each other so much, we have to put them all here.	*)

vectorp = ↑vector;
transp = ↑trans;
strngp = ↑strng;
eventp = ↑event;
framep = ↑frame;
statementp = ↑statement;
varidefp = ↑varidef;
nodep = ↑node;
identp = ↑ident;
tokenp = ↑token;
reswordp = ↑resword;
pdbp = ↑pdb;
envheaderp = ↑envheader;
enventryp = ↑enventry;
environp = ↑environment;
cmoncbp = ↑cmoncb;
messagep = ↑integer;
linerecp = ↑linerec;


(* datatype definitions *)

datatypes = (pconstype, varitype, svaltype, vectype, rottype, transtype,
	     frametype, eventtype, strngtype, labeltype, proctype, arraytype,
	     reftype, valtype, cmontype, nulltype, undeftype,
	     dimensiontype, mactype, macargtype, freevartype);

scalar = real;
vector = record  refcnt: integer; val: array [1..3] of real end;
trans = record  refcnt: integer; val: array [1..3,1..4] of real end;

cstring = packed array [1..10] of ascii;
c4str = packed array [1..4] of ascii;
c5str = packed array [1..5] of ascii;
c20str = packed array [1..20] of ascii;
linestr = packed array [1..130] of ascii;

strng = record
	  next: strngp;
	  ch: cstring;
	end;

event = record
	  next: eventp;		(* all events are on one big list *)
	  count: integer;
	  waitlist: pdbp;
	end;

frame = record
	  vari: varidefp;	(* back pointer to variable name & info *)
	  calcs: nodep;		(* affixment info *)
	  case ftype: boolean of	(* frame = true, device = false *)
  true:	    (valid: integer; val, fdepr: transp; dcntr: integer; dev: framep);
  false:    (mech: integer; case sdev: boolean of
		true: (sdest: real); false: (tdest,appr,depr: transp));
		(* sdev = true for scalar devices, false for frames *)
	end;


(* statement definitions *)

stmntypes = (progtype, blocktype, coblocktype, endtype, coendtype,
		fortype, iftype, whiletype, untiltype, casetype,
		calltype, returntype,
		printtype, prompttype, pausetype, aborttype, assigntype,
		signaltype, waittype, enabletype, disabletype, cmtype,
		affixtype, unfixtype,
		movetype,jtmovetype,operatetype,opentype,closetype,centertype,
		floattype, stoptype, retrytype,
		requiretype, definetype, macrotype, commenttype, dimdeftype,
		setbasetype, wristtype, saytype, declaretype, emptytype,
		evaltype, armmagictype);
		(* more??? *)

statement = packed record
		next, last: statementp;
		stlab: varidefp;
		exprs: nodep;	(* any expressions used by this statement *)
		nlines: integer;
		bpt,bad: boolean;
		case stype: stmntypes of

    progtype:	    (pcode: statementp; errors: integer);
    blocktype,
    declaretype,
    endtype,
    coendtype:	    (bcode, bparent: statementp; blkid: identp;
			level, numvars: 0..255; variables: varidefp);
		end;



(* auxiliary definitions: variable, etc. *)

varidef = packed record
	    next,dnext: varidefp;
	    name: identp;
	    level: 0..255;	(* environment level *)
	    offset: 0..255;	(* environment offset *)
	    dtype: varidefp;	(* to hold the dimension info *)
	    tbits: 0..15;  (* special type bits: array = 1, proc = 2, ref = 4 & ? *)
	    dbits: 0..15;	(* for use by debugger/interpreter *)
	    case vtype: datatypes of
  arraytype:  (a: nodep);
  proctype:   (p: nodep);
  labeltype,
  cmontype:   (s: statementp);
  mactype:    (mdef: statementp);
  macargtype: (marg: tokenp);
  pconstype:  (c: nodep);
  dimensiontype: (dim: nodep);
	  end;



(* definition of the ubiquitous NODE record *)

nodetypes = (exprnode, leafnode, listnode, clistnode, colistnode, forvalnode,
		deprnode, viaptnode, apprnode, destnode, byptnode, durnode,
		sfacnode, wobblenode, swtnode, nullingnode, wristnode, cwnode,
		arrivalnode, departingnode,
		ffnode, forcenode, stiffnode, gathernode, cmonnode, errornode,
		calcnode, arraydefnode, bnddefnode, bndvalnode,
		waitlistnode, procdefnode, tlistnode, dimnode, commentnode,
		linearnode, elbownode, shouldernode, flipnode, wrtnode,
		loadnode,velocitynode);

exprtypes =  (	svalop,					(* scalar operators *)
		sltop, sleop, seqop, sgeop, sgtop, sneop,	(* relations *)
		notop, orop, xorop, andop, eqvop,		(* logical *)
		saddop, ssubop, smulop, sdivop, snegop, sabsop, (* scalar ops *)
		sexpop, maxop, minop, intop, idivop, modop,
		sqrtop, logop, expop, timeop,			(* functions *)
		sinop, cosop, tanop, asinop, acosop, atan2op,	(* trig *)
		vdotop, vmagnop, tmagnop,
		vecop,					(* vector operators *)
		vmakeop, unitvop, vaddop, vsubop, crossvop, vnegop,
		svmulop, vsmulop, vsdivop, tvmulop, wrtop,
		tposop, taxisop,
		transop,				(* trans operators *)
		tmakeop, torientop, ttmulop, tvaddop, tvsubop, tinvrtop,
		vsaxwrop, constrop, ftofop, deproachop, fmakeop, vmkfrcop,
		ioop,					(* i/o operators *)
		queryop, inscalarop,
		specop,					(* special operators *)
		arefop, callop, grinchop, macroop, vmop, adcop, dacop, jointop,
		badop,
		addop, subop, negop, mulop, divop, absop); (* for parsing *)

leaftypes = pconstype..strngtype;

reltypes = sltop..sgtop;
forcetypes = (force,absforce,torque,abstorque,angvelocity);

node = record
	next: nodep;
	case ntype: nodetypes of
    exprnode:	(op: exprtypes; arg1, arg2, arg3: nodep; elength: integer);
    leafnode:	(case ltype: leaftypes of
	varitype:  (vari: varidefp; vid: identp);
	pconstype: (cname: varidefp; pcval: nodep);
	svaltype:  (s: scalar; wid: integer);
	vectype:   (v: vectorp);
	transtype: (t: transp);
	strngtype: (length: integer; str: strngp) ); (* also used by commentnodes *)
    listnode:	(lval: nodep);
    tlistnode:	(tok: tokenp);
	end;



(* records for parser: ident, token, resword *)

ident = record
	    next: identp;
	    length: integer;
	    name: strngp;
	    predefined: varidefp;
	  end;


tokentypes = (reswdtype, identtype, constype, comnttype, delimtype, labeldeftype,
		macpartype);

constypes = svaltype..strngtype;

reswdtypes = (stmnttype, filtype, clsetype, decltype, optype, edittype);

filtypes = (abouttype,alongtype,attype,bytype,defertype,dotype,elsetype,
		errmodestype,fromtype,handtype,intype,nonrigidlytype,rigidlytype,
		sourcefiletype,steptype,thentype,totype,untltype,viatype,
		withtype,worldtype,zeroedtype,oftype,wheretype,nowaittype,
		ontype,offtype,ppsizetype,collecttype,alltype,lextype,
		notype,righttype,lefttype,uptype,downtype,motiontype);

clsetypes = (approachtype,arrivaltype,departuretype,departingtype,durationtype,
		errortype,forcetype,forceframetype,forcewristtype,gathertype,
		nildeproachtype,nullingtype,stiffnesstype,
		torquetype,velocitytype,wobbletype,
		cwtype,ccwtype,stopwaittimetype,angularvelocitytype,
		respecttype,elbowtype,shouldertype,fliptype,lineartype,
		jointspacetype,loadtype);

edittypes = (getcmd,savecmd,insertcmd,renamecmd,startcmd,gocmd,proceedcmd,
		stepcmd,sstepcmd,nstepcmd,gstepcmd,executecmd,setcmd,tracecmd,
		breakcmd,unbreakcmd,tbreakcmd,definecmd,markcmd,unmarkcmd,
		popcmd,atcmd,calibratecmd);

token = record
	  next: tokenp;
	  case ttype: tokentypes of
constype:   (cons: nodep);
comnttype:  (len: integer; str: strngp);
delimtype:  (ch: ascii);
reswdtype:  (case rtype: reswdtypes of
	stmnttype: (stmnt: stmntypes);
	filtype:   (filler: filtypes);
	clsetype:  (clause: clsetypes);
	decltype:  (decl: datatypes);
	optype:	   (op: exprtypes);
	edittype:  (ed: edittypes) );
identtype:  (id: identp);
labeldeftype: (lab: varidefp);
macpartype: (mpar: varidefp);
	end;


resword = record
	  next: reswordp;
	  length: integer;
	  name: strngp;
	  case rtype: reswdtypes of
	stmnttype:  (stmnt: stmntypes);
	filtype:    (filler: filtypes);
	clsetype:   (clause: clsetypes);
	decltype:   (decl: datatypes);
	optype:	    (op: exprtypes);
	edittype:  (ed: edittypes);
	  end;



(* process descriptor blocks & environment record definitions *)

queuetypes = (nullqueue,nowrunning,runqueue,inputqueue,eventqueue,sleepqueue,
		forcewait,devicewait,joinwait,proccall);

pdb = packed record
	nextpdb,next: pdbp;	(* for list of all/active pdb's *)
	level: 0..255;		(* lexical level *)
	mode: 0..255;		(* expression/statement/sub-statement *)
	priority: 0..255;	(* probably never greater than 3? *)
	status: queuetypes;	(* what are we doing *)
	env: envheaderp;
	spc: statementp;	(* current statement *)
	epc: nodep;		(* current expression (if any) *)
	sp: nodep;		(* intermediate value stack *)
	cm: cmoncbp;		(* if we're a cmon point to our definition *)
	mech: framep;		(* current device being used *)
	linenum: integer;	(* used by editor/debugger *)
	 case procp: boolean of	(* true if we're a procedure *)
true:  (opdb: pdbp;		(* pdb to restore when procedure exits *)
	pdef: nodep);		(* procedure definition node *)
false: (evt: eventp;		(* event to signal when process goes away *)
	sdef: statementp);	(* first statement where process was defined *)
      end;


envheader = packed record
	      parent: envheaderp;
	      env: array [0..4] of environp;
	      varcnt: 0..255;		(* # of variables in use ??? *)
		case procp: boolean of  (* true if we're a procedure *)
	true: (proc: nodep);
	false:(block: statementp);
	    end;

enventry = record
	    case etype: datatypes of
  svaltype:  (s: scalar);
  vectype:   (v: vectorp);
  transtype: (t: transp);
  frametype: (f: framep);
  eventtype: (evt: eventp);
  strngtype: (length: integer; str: strngp);
  cmontype:  (c: cmoncbp);
  proctype:  (p: nodep; penv: envheaderp);
  reftype:   (r: enventryp);
  arraytype: (a: envheaderp; bnds: nodep);
	   end;


environment = record
		next: environp;
		vals: array [0..9] of enventryp;
	      end;


cmoncb = record
	   running, enabled: boolean;		(* cmon's status *)
	   cmon: statementp;
	   pdb: pdbp;
	   evt: eventp;
	   fbits: integer;			(* bits for force sensing *)
	   oldcmon: cmoncbp;			(* for debugger *)
	 end;



(* print related records: *)

cursorp = record
	  cline,ind: integer;
	  case stmntp: boolean of
  true:    (st: statementp);
  false:   (nd: nodep);
	 end;

linerec = record
	next: linerecp;
	start,length: integer
       end;

listingarray = packed array [0..listinglength] of ascii;



(* global variables *)

var
	(* from EDIT *)
    listing: listingarray;  (* first 150 chars are used by expression editor *)
			    (* next 40 by header & trailer lines *)
{*} cursorStack: array [1..15] of cursorp;	{These are BIG records! }
    lbuf: array [1..160] of ascii;
    ppBuf: array [1..100] of ascii;
    lines: array [1..maxLines] of linerecp; (* what's on the screen + some *)
    ppLines: array [1..maxPPLines] of linerecp;	(* for page printer *)
    marks: array [1..20] of integer;
    reswords: array [0..26] of reswordp;
    idents: array [0..26] of identp;
    macrostack: array [1..10] of tokenp;
    curmacstack: array [1..10] of varidefp;
    screenheight,dispHeight: integer;
    ppBufp,oppBufp,ppOffset,ppSize,nmarks: integer;
    lbufp,cursor,ocur,cursorLine,fieldnum,lineNum,findLine,pcLine: integer;
    firstDline,topDline,botDline,firstLine,lastLine,curLine: integer;
    freeLines,oldLines: linerecp;
    sysVars: varidefp;
    dProg: statementp;
    curBlock, newDeclarations, findStmnt: statementp;
    macrodepth: integer;
    filedepth, errCount, sCursor: integer;
    curChar, maxChar, curFLine, curPage: integer;
    nodim, distancedim, timedim, angledim,
      forcedim, torquedim, veldim, angveldim: varidefp;
    fvstiffdim, mvstiffdim: nodep;
    pnode: nodep;
    smartTerminal: boolean; (* true = insert/delete, false = redraw line *)
    setUp,setExpr,setCursor,dontPrint,outFilep,collect,fParse,sParse,
      eofError,endOfLine,backup,expandmacros,flushcomments,checkDims,
      shownLine: boolean;
    curtoken: token;
    file1,file2,file3,file4,file5,outFile: atext;

    bpts: array [1..maxBpts] of statementp;	(* debugging crap *)
    tbpts: array [1..maxTBpts] of statementp;
    debugPdbs: array [0..10] of pdbp;
    nbpts,ntbpts,debugLevel: integer;
    eCurInt: pdbp;
    STLevel: integer;		(* set by GO *)
    singleThreadMode,tSingleThreadMode: boolean;

	(* from INTERP *)
    inputLine: array [1..20] of ascii;
    talk: text;			(* for using the speech synthesizer *)
    curInt, activeInts, readQueue, allPdbs: pdbp;
    sysEnv: envheaderp;
    clkQueue: nodep;
    allEvents: eventp;
    etime: integer;		(* used by eval *)
    curtime: integer; (* who knows where this will get updated - an ast? *)
    stime: integer;		(* used for clock queue on 10 *)
    msg: messagep;		(* for AL-ARM interaction *)
    inputp: integer;		(* current offset into inputLine array above *)
    resched, running, escapeI, iSingleThreadMode: boolean;
    msgp: boolean;		(* flag set if any messages pending *)
    inputReady: boolean;

(* various constant pointers *)
    xhat,yhat,zhat,nilvect: vectorp;
    niltrans: transp;
    gpark, rpark: transp;		(* arm park positions *)

(* various device & variable pointers *)
    speedfactor: enventryp;
    garm: framep;

{ Externally defined routines from elsewhere: }

	(* From ALLOC *)
procedure relVector(v: vectorp);				external;

	(* From EROOT *)
function epTaxis (t: transp): vectorp; 				external;
function epTmagn (t: transp): scalar; 				external;

	(* From PP *)
function getLine(length: integer): linerecp;			external;
procedure relLine(l: linerecp);					external;
procedure ppLine; 						external;
procedure ppOutNow; 						external;
procedure ppChar(ch: ascii); 					external;
procedure pp5(ch: c5str; length: integer); 			external;
procedure pp10(ch: cstring; length: integer); 			external;
procedure pp10L(ch: cstring; length: integer);			external;
procedure pp20(ch: c20str; length: integer); 			external;
procedure pp20L(ch: c20str; length: integer); 			external;
procedure ppInt(i: integer); 					external;
procedure ppStrng(length: integer; s: strngp); 			external;

	(* From DISP *)
procedure beep; 						external;
procedure outLine(line,col,start,length: integer); 		external;


procedure eputget; external;
procedure ePutGet; begin end;
(* print routines: putChar, put5, put10, putLine *)

procedure putChar(ch: ascii); external;
procedure putChar;
 var i: integer; l: linerecp;

    procedure out1Line(line,start,length: integer);	(* Also in EAUX1A *)
     begin
     if length > 79 then length := 79;      (* only display first 79 chars *)
     outLine(line,1,start,length);
     end;

 begin
 if ch = chr(15B) then
   begin			(* write out the line *)
   if lbufp > 160 then lbufp := 160;	(* in case there was an overflow *)
   if outFilep then
     begin			(* send line out to file *)
     for i := 1 to lbufp do write(outFile,lbuf[i]);
     writeln(outFile);		(* don't forget the crlf *)
     end
    else
     if (firstLine <= curLine) and (curLine <= lastLine) then
      if not (setup or dontPrint or fParse) then
       begin
       l := getLine(lbufp);		(* get a line to store chars in *)
       for i := 1 to lbufp do listing[l↑.start+i-1] := lbuf[i]; (* copy line *)
       for i := lbufp to l↑.length-1 do listing[l↑.start+i] := chr(0);
       i := curLine - topDline + 1;		(* index into lines array *)
       if lines[i] <> nil then
	 begin					(* complain *)
	 if ppBufp > 0 then ppLine;
	 pp5('Line ',5); ppInt(i-firstDline+1);
	 pp20(' wasn''t freed!  Old ',20); pp5('len: ',5);
	 ppInt(lines[i]↑.length);
	 pp10(' New len: ',10); ppInt(l↑.length); ppLine;
	 relLine(lines[i]);
	 end;
       lines[i] := l;				(* add to display list *)
       i := i - firstDline + 1;			(* where it goes on screen *)
       if (0 < i) and (i <= dispHeight) then
	 out1Line(i,l↑.start,lbufp);		(* & display it *)
       end;
   curLine := curLine + 1;
   lbufp := 0;
   end
  else if ch <> chr(12B) then	(* flush linefeeds *)
   begin			(* add character to line buffer *)
   lbufp := lbufp + 1;
   if lbufp > 160 then
     begin
     if lbufp = 161 then
       begin pp20L('Line buffer overflow',20); ppLine; end
     end
    else lbuf[lbufp] := ch;
   end;
 end;

procedure put5(ch: c5str; length: integer); external;
procedure put5;
 var i: integer;
 begin
 for i := 1 to length do putChar(ch[i]);
 end;

procedure put10(ch: cstring; length: integer); external;
procedure put10;
 var i: integer;
 begin
 for i := 1 to length do putChar(ch[i]);
 end;

procedure putLine; external;
procedure putLine;
 begin
 putChar(chr(15B));		(* cr *)
 end;

(* aux print routines: putReal, putInt, putVec, putTrans, putStrng, putTlist *)

procedure putReal (s: real); external;
procedure putReal ;
 var i,j,si,expo: integer; sf: real;
 begin
 if s < 0 then begin putchar('-'); s := -s end;
 if s < 1E-20 then begin expo := 0; s := 0 end
  else
   begin 
   expo := trunc(ln(s)/ln(10.0));	(* how big is s? *)
   s := s / exp(expo * ln(10));	(* normalize it between 1.0 & 9.999... *)
   s := s + 0.0000005;			(* round it off too *)
   end;
 sf := 0.000001;
 i := 0;
 while (expo >= 0) and (i < 7) do
  begin
  si := trunc(s);			(* get next digit *)
  putchar(chr(ord('0') + si));
  s := 10.0 * (s - si);
  sf := 10 * sf;
  expo := expo - 1;
  i := i + 1;
  end;
 if expo > 0 then
   begin
   for j := 1 to expo do putchar('0'); 	(* print trailing zeros *)
   end
  else
   begin				(* deal with fractional part *)
   if i = 0 then putchar('0');
   if s > sf then putchar('.');
   for j := 1 to -expo-1 do putchar('0');  (* print leading zeros, if any *)
   while (s > sf) and (i < 7) do
    begin
    si := trunc(s);			(* get next digit *)
    putchar(chr(ord('0') + si));
    s := 10.0 * (s - si);
    sf := 10 * sf;
    i := i + 1;
    end;
   end;
 end;

procedure putInt(r: real); external;
procedure putInt;
 begin
 putReal(round(r));
 end;

procedure putVec(v: vectorp); external;
procedure putVec;
 var i: integer;
 begin
 put10('vector(   ',7);
 with v↑ do 
  for i := 1 to 3 do 
   begin
   putReal(val[i]);
   if i = 3 then putChar(')') else putChar(',');
   end;
 end;

procedure putTrans(t: transp); external;
procedure putTrans;
 var i: integer; v: vectorp;
 begin
 with t↑ do
  begin
  refcnt := refcnt + 1;
  put10('trans(rot(',10);
  v := eptaxis(t); putVec(v); relVector(v);
  putChar(',');
  putReal(eptmagn(t));
  put10(' * degrees',10); put10('),vector( ',9);
  for i := 1 to 3 do
   begin putReal(val[i,4]); if i = 3 then putChar(')') else putChar(',') end;
  put10(' * inches)',10);
  refcnt := refcnt - 1;
  end;
 end;

procedure putStrng(length: integer; s: strngp); external;
procedure putStrng;
 var i,j: integer;
 begin
 j := 1;
 for i := 1 to length do
  begin
  putchar(s↑.ch[j]);
  if j = 10 then begin j := 1; s := s↑.next; end
   else j := j + 1;
  end;
 end;

procedure putTlist(t: tokenp); external;
procedure putTlist;
 var b: boolean; i: integer; r: reswordp;

    (* Swiped from PARSE, since it's only called by EDIT *)
    function findResword(what: reswdtypes; which, where: integer): reswordp;
     var b: boolean; i: integer; r: reswordp;
     begin
     b := true;
     i := where;
     while b and (i<=26) do
      begin         (* try to find reserved word & print it out *)
      r := reswords[i];     (* try next bucket *)
      while b and (r <> nil) do
       with r↑ do
	if (what=rtype) and (which = ord(stmnt)) then b := false else r := next;
      i := i + 1;
      end;
     findResword := r;
     end;

 begin (* putTList *)
 while t <> nil do
  begin
  with t↑ do
   case ttype of
reswdtype:  begin
	    if (rtype=stmnttype) or
	       ((rtype=filtype) and
		((filler=withtype) or (filler=untltype) or (filler=viatype))) then
	      begin
	      putline;
	      put10('          ',10);
	      end
	     else putchar(' ');
	    r := findResword(t↑.rtype,ord(t↑.stmnt),0);
	    if r <> nil then putStrng(r↑.length,r↑.name);
	    end;
identtype:  begin
	    putchar(' ');
	    putstrng(id↑.length,id↑.name);
	    end;
macpartype: begin
	    putchar(' ');
	    with mpar↑.name↑ do putstrng(length,name);
	    end;
constype:   if cons↑.ltype = svaltype then
	      begin putchar(' '); putReal(cons↑.s) end
	     else
	      begin
	      put5(' "   ',2);
	      putstrng(cons↑.length,cons↑.str);
	      putchar('"');
	      end;
comnttype:  begin
	    putchar(' ');
	    putstrng(len,str);
	    end;
delimtype:  putchar(ch);
otherwise  {do nothing};
   end;
  t := t↑.next;
  end;
 end (* putTList *);

(* expression related routine: getExprLength *)

function getExprLength(n: nodep): integer; external;
function getExprLength;
 var i: integer;
 begin
 if n = nil then i := 10
  else
   with n↑ do
    if ntype = exprnode then i := elength
     else if ntype = leafnode then
      case ltype of
varitype:  i := vid↑.length;
pconstype: i := cname↑.name↑.length;
svaltype:  i := wid;
strngtype: i := length + 2;
vectype,
transtype: i := 99;	(* who knows??? *)
otherwise {do nothing};
       end
     else i := 0;		(* who knows how long it is *)
 getExprLength := i;
 end;

(* Expression output routine:  putExpr *)

procedure putexpr(n: nodep; opp: integer); external;
procedure putexpr;
 var rp, parg1, parg2: boolean; curp: integer; pn: nodep;

procedure lp;
 begin
 if curp < opp then begin putchar('('); rp := true end else rp := false;
 end;

 begin
 if n = nil then put10('/* expr */',10) else
 with n↑ do
  begin
  if ntype = leafnode then
    case ltype of
 svaltype:  putReal(s);
 vectype:   putVec(v);
 transtype: putTrans(t);
 strngtype: begin
	    putchar('"');
	    putStrng(length,str);
	    putchar('"');
	    end;
 varitype:  with vid↑ do putStrng(length,name);
 pconstype: with cname↑.name↑ do putStrng(length,name);
 otherwise {do nothing};
    end
   else			(* it must be an expression node *)
    begin
    rp := false;
    parg2 := false;
    parg1 := true;
    case op of
eqvop:	begin curp := 1; lp; putexpr(arg1,1); parg2 := true;
	 put5(' eqv ',5) end;
orop,
xorop:	begin curp := 2; lp; putexpr(arg1,2); parg2 := true;
	 if op = xorop then put5(' xor ',5)
	  else put5(' or  ',4) end;
andop:	begin curp := 3; lp; putexpr(arg1,3); parg2 := true;
	 put5(' and ',5) end;
sltop,
sleop,
seqop,
sgeop,
sgtop,
sneop:	begin curp := 4; lp; putexpr(arg1,4); parg2 := true;
	 case op of
    sltop: put5(' <   ',3);
    sleop: put5(' <=  ',4);
    seqop: put5(' =   ',3);
    sgeop: put5(' >=  ',4);
    sgtop: put5(' >   ',3);
    sneop: put5(' <>  ',4);
	  end;
	end;
saddop,
ssubop,
vaddop,
vsubop,
tvaddop,
tvsubop: begin curp := 5; lp; putexpr(arg1,5); parg2 := true;
	 if (op=saddop) or (op=vaddop) or (op=tvaddop) then put5(' +   ',3)
	  else put5(' -   ',3) end;
wrtop:	begin curp := 6; lp; putexpr(arg1,6); parg2 := true; put5(' wrt ',5); end;
smulop,
sdivop,
maxop,
minop,
idivop,
modop,
vdotop,
svmulop,
vsmulop,
vsdivop,
crossvop,
tvmulop,
ttmulop: begin curp := 7; lp; putexpr(arg1,7); parg2 := true;
	 if op = vdotop then put5(' .   ',3) else
	  if op = maxop then put5(' max ',5) else
	  if op = minop then put5(' min ',5) else
	  if op = idivop then put5(' div ',5) else
	  if op = modop then put5(' mod ',5) else
	  if (op=sdivop) or (op=vsdivop) then put5(' /   ',3)
	   else put5(' *   ',3);
	 end;
sexpop,
ftofop:	begin curp := 8; lp; putexpr(arg1,8); parg2 := true;
	 if op = sexpop then put5(' ↑   ',3) else put5(' ->  ',4); end;
intop:	put5('int( ',4);
unitvop: put5('unit(',5);
sqrtop:	put5('sqrt(',5);
expop:	put5('exp( ',4);
logop:	put5('log( ',4);
timeop:	begin
	put10('runtime   ',7);
	with arg1↑ do
	 if (ntype <> leafnode) or (ltype <> svaltype) or (s <> 0.0) then
	   putchar('(')
	  else parg1 := false;
	end;
sinop:	put5('sin( ',4);
cosop:	put5('cos( ',4);
tanop:	put5('tan( ',4);
asinop:	put5('asin(',5);
acosop:	put5('acos(',5);
tposop:	put5('pos( ',4);
taxisop: put5('axis(',5);
specop: putchar('(');
negop,	(* this and above used in dimension statement *)
tinvrtop: put5('inv( ',4);
torientop: put10('orient(   ',7);
deproachop: put10('deproach( ',9);
adcop:	 put5('adc( ',4);
dacop,
atan2op,
vsaxwrop,
tmakeop,
fmakeop,
constrop,
vmakeop: begin parg1 := false;
	 if op = atan2op then put10('atan2(    ',6) else
	  if op = vsaxwrop then put5('rot( ',4) else
	  if op = tmakeop then put10('trans(    ',6) else
	  if op = fmakeop then put10('frame(    ',6) else
	  if op = vmakeop then put10('vector(   ',7) else
	  if op = constrop then put10('construct(',10)
	   else put5('dac( ',4);
	 putexpr(arg1,0); putchar(','); putexpr(arg2,0);
	 if (op=vmakeop) or (op=constrop) then
	  begin putchar(','); putexpr(arg3,0) end;
	 putchar(')');
	 end;
sabsop,
vmagnop,
tmagnop: begin parg1 := false; putchar('|'); putexpr(arg1,0); putchar('|') end;
inscalarop: begin parg1 := false; put10('inscalar  ',8) end;
grinchop: begin parg1 := false; putchar('#') end;
snegop,
vnegop,
notop:	begin parg1 := false;
	if op = notop then put5(' not ',5) else put5(' -   ',2);
	putexpr(arg1,9)
	end;
queryop,
arefop,
jointop,
macroop,
callop:	begin parg1 := false;
	if op = queryop then 
	  begin put5('query',5);
	   if arg2 <> nil then putchar('(')
	  end
	 else begin
		with arg1↑.vid↑ do putStrng(length,name);
		if (op = arefop) or (op = jointop) then putchar('[')
		 else if arg2 <> nil then putchar('(')
	      end;
	pn := arg2;
	while pn <> nil do
	 begin
	 if op = macroop then putTlist(pn↑.tok) else putexpr(pn↑.lval,0);
	 pn := pn↑.next; 
	 if pn <> nil then putchar(',');
	 end;
	if (op = arefop) or (op = jointop) then putchar(']')
	 else if arg2 <> nil then putchar(')');
	end;
badop:	begin parg1 := false;
	put10('(*bad-*)  ',9);
	putexpr(arg1,0);
	put10('(*-bad*)  ',9);
	end;
otherwise {do nothing - shouldn't happen! };
     end;
    if parg2 then putexpr(arg2,curp)
     else if parg1 then begin putexpr(arg1,0); putchar(')') end;
    if rp then putchar(')');
    end;
  end;
 end;